package kwic.pf;

import java.io.CharArrayWriter;
import java.io.IOException;
import java.util.StringTokenizer;

public class LineTransformer extends Filter {

	public LineTransformer(Pipe input, Pipe output) {
		super(input, output);
	}

	@Override
	protected void transform() {
		try {
			CharArrayWriter writer = new CharArrayWriter();

			int c = input_.read();
			while (c != -1) {
				if (((char) c) == '\n') {
					String line = writer.toString();
					StringTokenizer tokenizer = new StringTokenizer(line);
					String[] words = new String[tokenizer.countTokens()];
					int i = 0;
					while (tokenizer.hasMoreTokens())
						words[i++] = tokenizer.nextToken();

					String shift = words[0].toUpperCase();
					for (i = 1; i < words.length; i++) {
						shift += " " + words[i];
					}
					shift += '\n';
					char[] chars = shift.toCharArray();
					for (int j = 0; j < chars.length; j++)
						output_.write(chars[j]);
					writer.reset();
				} else
					writer.write(c);
				c = input_.read();
			}
			output_.closeWriter();
		} catch (IOException exc) {
			exc.printStackTrace();
			System.err.println("KWIC Error: Could not make LineTransformer.");
			System.exit(1);
		}
	}

}
